package com.googlecode.astoolkit.modules.content.model {
	
	import com.googlecode.astoolkit.modules.content.section.ISection;
	import com.googlecode.astoolkit.utils.AbstractNode;
	import com.googlecode.astoolkit.utils.INode;
	import com.googlecode.astoolkit.utils.Node;
	import com.googlecode.astoolkit.utils.NullNode;
	import com.googlecode.astoolkit.utils.Sitemap;

	public class ContentSitemap {
		
		private static var _instance:ContentSitemap;
		private static var _sitemap:ContentSitemap;
		private var _data:XML;
		private var _nodes:Array;

		public function ContentSitemap(singletonenforcer:SingletonEnforcer){
			if (singletonenforcer == null) {
				throw new Error("Sitemap es un Singleton. Para acceder a una instancia hacedlo mediante Sitemap.getInstance()");
				return ;
			}
			
			_nodes = new Array();
		}
		
		public static function getInstance():ContentSitemap {
			if (!_instance) {
				_instance = new ContentSitemap(new SingletonEnforcer());
			}

			return _instance;
		}
		
		//
		/* public function data(rawXml:XML):void{
			_data = setIds(rawXml);
			createNodes(_data);
		} */
		
		/* public static function getNodeById(id:int):INode{
			var node:INode; 
			if(id!=-1){
				node = ContentSitemap.nodes[id];
			}else{
				node = new NullNode();
			}
			//trace("getNodeById-->" + node);
			return node;
		} */
		
		public static function getInstanceById(id:int):ISection{
			return AbstractNode(Sitemap.getNodeById(id)).instance;
		}
		
		public static function getInstanceByPath(path:String):ISection{
			return AbstractNode(Sitemap.getNodeByPath(path)).instance;
		}
		
		//
		/* public static function getNodeByPath(path:String):INode{
			var nodes:Array = Sitemap.nodes;
			for each(var n:AbstractNode in nodes){
				if(n.path == path){
					return n;
				}
			}
			return new NullNode();
		} */
		
		public static function getNodeByInstance(instance:ISection):INode{
			var nodes:Array = ContentSitemap.nodes;
			for each(var n:AbstractNode in nodes){
				if(n.instance == instance){
					return n;
				}
			}
			return new NullNode();
		}
		
		/* public static function getIdByPath(path:String):int{
			return Sitemap.getNodeByPath(path).id;
		} */
		
		/* public static function getIdsArrayByPath(path:String):Array{
			var arr:Array = new Array();
			loop(Sitemap.getNodeByPath(path));
			function loop(node:INode):void{
				if(node is Node){
					arr.splice(0,0, node.id)
					loop(node.parent);
				}
			}
			return arr;
		} */
		
		// Getter y Setter de _data
		public static function get xml():XML{
		     return ContentSitemap.getInstance()._data;
		}
		
		// Getter de _nodes
		public static function get nodes():Array{
		     return ContentSitemap.getInstance()._nodes;
		} 
		
		// Getter de _defaultPath
		public static function get defaultPath():String{
			var path:String = "";
			if(ContentSitemap.xml.hasOwnProperty("@defaultPath")){
				path = ContentSitemap.xml.@defaultPath;
			}
		      return path;
		}
		

/*****************************************
 Private Functions
******************************************/


		
		// Inyecta los ids
		/*  private function setIds(data:XML):XML {
			var id:uint = 0;

			//_data = new XML(data);
			data.@id = id++;
			loop(data);

			function loop(branch:XML):void {
				var l:uint = branch.children().length();

				for(var i:uint = 0; i < l; i++) {
					var subBranch:XML = branch.children()[i];
					subBranch.@id = id++;
					// si la rama tiene hijos lo hago recursivo
					if (subBranch.children().length() > 0) {
						loop(subBranch);
					}
				}
			}
			return data;
		}
		
		
		private function createNodes(data:XML):void {
			addNode(0, new MainNode(data));
			loop(data);

			function loop(branch:XML):void {
				for each(var subBranch:XML in branch.section) {
					addNode(subBranch.@id, new Node(subBranch));
					if (subBranch.children().length() > 0) {
						loop(subBranch);
					}
				}
			}
		}


		private static function addNode(id:int, node:AbstractNode):void {
			ContentSitemap.getInstance()._nodes[id] = node;
		} */
		
		

	}
}

class SingletonEnforcer {}